package com.example.services;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.exceptions.ItemNotFoundException;
import com.example.exceptions.ProductNotFoundException;
import com.example.models.Cart;
import com.example.models.ItemToPurchase;
import com.example.models.Product;
import com.example.repositories.ItemToPurchaseRepository;
import com.example.repositories.ProductRepository;

@Service
public class ItemToPurchaseService {
	@Autowired
	ItemToPurchaseRepository itemToPurchaseRepository;
	@Autowired
	ProductService productService;
	
	public void checkout(String itemId){
		ItemToPurchase item = this.retrieveItemOrError(itemId);
		productService.checkout(String.valueOf(item.getProduct_id()),item.getQuantity());
		//update cart condition to 'bought'
	}
	
	public ItemToPurchase retrieveItemOrError(String itemId){
		ItemToPurchase result = itemToPurchaseRepository.findOne(String.valueOf(itemId));
		if(result==null) throw new ItemNotFoundException(itemId);
		return result;
	}
}
